package com.espirit.finance.core;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.googlecode.cswish.annotation.GenericType;
import com.googlecode.cswish.annotation.ParamScope;
import com.googlecode.cswish.model.PageElement;
import com.googlecode.cswish.model.PageElements;
import com.googlecode.cswish.model.PageInfo;
import com.googlecode.cswish.model.Reference;
import com.googlecode.cswish.oosearch.QLInfo;
import com.googlecode.cswish.struts.spring.GenericService;
import com.googlecode.cswish.util.bean.ModelUtil;

@Service
public class AcctItemService {
	@Resource
	protected GenericService genericService;
	
	
	
	public  PageInfo search(final AcctItem model, int start, int limit,
    		String sort, String dir, boolean newSearch, Reference<QLInfo> qlInfoRef,
    		@ParamScope(visible=false, value="#ps")PageElements ps ,boolean fromMenu,
    		Reference<String> parentId) {
		if (model != null && model.getParent() != null && model.getParent().getId() != null){
			parentId.set(model.getParent().getId());
		} else {
			if (parentId != null){
				ModelUtil.setProperty(model, "parent.id", parentId.get());
			}
		}
		PageInfo results = genericService.search(model, start, limit, sort, dir, true, qlInfoRef);
		formatData(results, ps);
		return results;
	}
	
	protected void formatData(PageInfo results, PageElements ps){
	}
	
	public void _add(AcctItem model, @ParamScope(visible=false, value="#ps")PageElements ps, Reference<Integer> parentId){
		if (parentId != null){
			Integer id = parentId.get();
			if (id != null){
				Acct acct = genericService.load(Acct.class, id);
				ModelUtil.setProperty(model, "parent", acct);
				ps.setElementReadOnly("model.parent.id", true);
				
				setElementProperty(ps, acct);
				
			}
		}
	}
	
	public AcctItem add(AcctItem model, Reference<Integer> parentId){
		if (parentId != null){
			Integer id = parentId.get();
			if (id != null){
				Acct acct = genericService.load(Acct.class, id);
				model.setParent(acct);
				return genericService.add(model);
			}
		}
		return null;
	}
	
	private void setElementProperty(PageElements ps, Acct acct){
		for(PageElement pe : ps.getAllElements().values()){
			if (pe.getName().startsWith("model.acctItemScopes.")){
				pe.setHidden(false);
			} else {
				pe.setHidden(false);
			}
		}
		
		Integer strategy = acct.getStrategy();
		if (strategy != null){
			if (Acct.STRATEGY_FIXED.equals(strategy)){
				ps.setElementVisible("model.acctItemRef.id", false);
//				ps.setElementVisible("model.acctItemFactor.id", false);
				ps.setElementVisible("model.style", false);
				ps.setElementVisible("model.triggerCondition", false);
				
				for(PageElement pe : ps.getAllElements().values()){
					if (pe.getName().startsWith("model.acctItemScopes.")){
						pe.setHidden(true);
					} 
				}
			} else if (Acct.STRATEGY_GENERAL.equals(strategy)){
				ps.setElementVisible("model.fixedAmount", false);
				ps.setElementVisible("model.conditionAmount", false);
			}
		}
	}
	
	public void _update(@GenericType Object model, @ParamScope(visible=false, value="#ps")PageElements ps){
		ps.setElementReadOnly("model.parent.id", true);
		if (model != null){
			AcctItem acctItem = (AcctItem) genericService.load(model);
			
			if (acctItem != null){
				Acct acct = acctItem.getParent();
				setElementProperty(ps, acct);
			}
		}
		genericService.view(model);
	}
	
	protected <T extends AcctItem> List<T> getValidAcctItems(T model){
		model.setActive(AcctItem.ACTIVE_YES);
		ModelUtil.setProperty(model, "parent.active", Acct.ACTIVE_YES);
		return genericService.searchAll(model).getData();
	}
	
	
}
